﻿@{
    Bundle("jquery_validate");
    Bundle("browserStorage");
    Bundle("browserInput");
    var stock = (Model.StockEntries as IEnumerable<dynamic>).FirstOrDefault();
    var options = (Model.Options as IEnumerable<dynamic>)
        .GroupBy(x => JDM.String(x.CodeName));
    bool variablePricing = JDM.HasFlags(Model.Flags, "VariablePricing");
}

<script type="text/javascript">
    jQuery(document).ready(function () {
        jQuery('@ID("#form")').validate({
            rules: {
                quantity: {
                    required: true
                },
                price: {
                    required: true
                },
                @foreach(var g in options.Where(x => x.Any(i => JDM.HasFlags(i.Flags, "Required"))))
                {
                    <text>
                    @g.Key: {
                        required: true
                    },
                    </text>
                }
            },
            submitHandler: function(form) {
                var opts = [];
                @foreach(string id in options.SelectMany(g => g.Select(i => ID("#opt_{0}_{1}", g.Key, JDM.Int32(i.EntityID)))))
                {
                    <text>
                    if (jQuery('@id').prop('selected') || jQuery('@id').prop('checked')) {
                        opts.push(jQuery('@id').val());
                    }
                    </text>
                }
                jQuery('@ID("#opts")').val(opts);
                form.submit();
            }
        });
        jQuery('@ID("#form")').browserInput({
            key: '@Par.In("UserStorage").Of("Input").Name(ViewID)',
            includes: 'input',
            excludes: 'input[type=hidden]',
            reset: '@((TempData.RestoreStatus() >= 0).ToString().ToLowerInvariant())'
        });
    });
</script>
<form id="@ID("form")" class="common-form" action="@Url.Action(Loc.Act("Put").Ctrl("ShoppingCart").Area("Wizard").Localized())" method="get">
    <input type="hidden" name="salesItemID" value="@JDM.Int32(Model.EntityID)" />
    @if(!variablePricing)
    {
        <input type="hidden" name="price" value="@Svc.Price((decimal)JDM.Decimal(Model.Price))" />
    }
    <input id="@ID("opts")" type="hidden" name="options" />
    <ul>
        <li>
            <label>@T(Res.Name("Price"))</label>
        </li>
        <li>
            <input type="text" name="price" @(variablePricing ? String.Empty : "readonly") value="@Svc.Price((decimal)JDM.Decimal(Model.Price))" />
        </li>
        <li>
            <label>@T(Res.Name("Quantity"))</label>
        </li>
        <li>
            <input type="text" name="quantity" value="@Svc.Numeric((decimal)(stock != null ? JDM.Decimal(stock.AdjustmentMin) : 1))" />
        </li>
        @foreach(var g in options)
        {
            bool required = g.Any(x => JDM.HasFlags(x.Flags, "Required"));
            bool multipleChoice = g.Any(x => JDM.HasFlags(x.Flags, "MultipleChoiceAllowed"));
            bool selectLayout = g.Count() > 4;
            var t_firstOption = (g.First().Translations as IEnumerable<dynamic>)
                .SingleOrDefault(x => JDM.Int32(x.Language.EntityID) ==
                    JDM.Int32(Svc.Localization.Language.EntityID));
            <li>
                <label>@(t_firstOption != null ? JDM.String(t_firstOption.Name) : g.Key)</label>
            </li>
            <li>
            @if(selectLayout)
            {
                @:<select name="@g.Key" @(multipleChoice ? "multiple" : String.Empty)>
                if(!required && !multipleChoice)
                {
                    <option value=""></option>
                }
            }
            @foreach(var i in g)
            {
                var t_option = (i.Translations as IEnumerable<dynamic>)
                    .SingleOrDefault(x => JDM.Int32(x.Language.EntityID) ==
                        JDM.Int32(Svc.Localization.Language.EntityID));
                int id = JDM.Int32(i.EntityID);
                string html_id = ID("opt_{0}_{1}", g.Key, id);
                bool defaultChoice = JDM.HasFlags(i.Flags, "DefaultChoice");
                string label = T(Res.Name("Option"), 
                    t_option != null
                        ? JDM.String(t_option.Value)
                        : JDM.String(i.CodeValue),
                    Svc.Price((decimal)JDM.Decimal(i.Price)));
                if(selectLayout)
                {
                    <option id="@html_id" value="@id" @(defaultChoice ? "selected" : String.Empty)>@label</option>
                } 
                else
                {
                    <input id="@html_id" type="@(multipleChoice || g.Count() == 1 ? "checkbox" : "radio")" name="@g.Key" value="@id" @(defaultChoice ? "checked" : String.Empty) /> @label<br/>
                }
            }  
            @if(selectLayout)
            {
                @:</select>
            }
            </li>
        }
    </ul>
    <ul class="bar toolbar">
    @if(!Svc.IsAuthenticated)
    {
        <li><a href="@Url.Action(Loc.Act("SignIn").Ctrl("Access").NoArea().Localized())">@T(Res.In("WebPortal").Of("Navigation").Of("Access").Name("SignIn"))</a></li>
        <li class="separator">|</li>
        <li><a href="@Url.Action(Loc.Act("SignUp").Ctrl("Access").NoArea().Localized())">@T(Res.In("WebPortal").Of("Navigation").Of("Access").Name("SignUp"))</a></li>
    }
    else if(stock != null && JDM.Decimal(stock.AdjustmentMin) > JDM.Decimal(stock.Reserve) &&
        !JDM.HasFlags(stock.Flags, "OversoldAllowed"))
    {
        <li><button class="button" type="submit" disabled>@T(Res.In("Common").Of("Act").Name("Submit"))</button></li>
    }
    else
    {
        <li><button class="button" type="submit">@T(Res.In("Common").Of("Act").Name("Submit"))</button></li>
    }
    </ul>
</form>